﻿package org.xhs.report.common.service
{
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class PageDataProvider extends EventDispatcher implements IPageDataProvider
    {
        protected var _isPost:Boolean = false;
        protected var _docId:String = "";
        protected var _docUrl:String = "http://wenku.baidu.com/play";
        protected var _docFpth:int = 0;
        protected var _docNpn:int = 5;
        protected var _docBduss:String = "";
        protected var _isLoading:Boolean = false;
        protected var _urlStream:URLStream;
        protected var _binaryData:ByteArray;

        public function PageDataProvider(param1:IEventDispatcher = null)
        {
            super(param1);
            this.init();
            return;
        }// end function

        protected function init() : void
        {
            this._binaryData = new ByteArray();
            this._binaryData.endian = Endian.LITTLE_ENDIAN;
            this._urlStream = new URLStream();
            this._urlStream.addEventListener(Event.COMPLETE, this.binaryLoadComplete);
            this._urlStream.addEventListener(ProgressEvent.PROGRESS, this.binaryLoading);
            this._urlStream.addEventListener(IOErrorEvent.IO_ERROR, this.binaryLoadError);
            this._urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.binaryLoadError);
            return;
        }// end function

        protected function binaryLoadComplete(event:Event) : void
        {
            this._isLoading = false;
            dispatchEvent(new PageDataEvent(PageDataEvent.LOAD_SUCCESS));
            return;
        }// end function

        protected function binaryLoading(event:ProgressEvent = null) : void
        {
            if (this._urlStream.connected && this._binaryData)
            {
                this._urlStream.readBytes(this._binaryData, this._binaryData.length);
                dispatchEvent(new PageDataEvent(PageDataEvent.LOAD_PROGRESS));
            }
            return;
        }// end function

        protected function binaryLoadError(event:Event = null) : void
        {
            dispatchEvent(new PageDataEvent(PageDataEvent.LOAD_FAIL));
            return;
        }// end function

        public function set isPost(param1:Boolean) : void
        {
            this._isPost = param1;
            return;
        }// end function

        public function get isPost() : Boolean
        {
            return this._isPost;
        }// end function

        public function set docId(param1:String) : void
        {
            this._docId = param1;
            return;
        }// end function

        public function get docId() : String
        {
            return this._docId;
        }// end function

        public function set docUrl(param1:String) : void
        {
            this._docUrl = param1;
            return;
        }// end function

        public function get docUrl() : String
        {
            return this._docUrl;
        }// end function

        public function set docFpth(param1:int) : void
        {
            this._docFpth = param1;
            return;
        }// end function

        public function get docFpth() : int
        {
            return this._docFpth;
        }// end function

        public function set docNpn(param1:int) : void
        {
            this._docNpn = param1;
            return;
        }// end function

        public function get docNpn() : int
        {
            return this._docNpn;
        }// end function

        public function set docBduss(param1:String) : void
        {
            this._docBduss = param1;
            return;
        }// end function

        public function get docBduss() : String
        {
            return this._docBduss;
        }// end function

        public function get binaryData() : ByteArray
        {
            return this._binaryData;
        }// end function

        public function get isLoading() : Boolean
        {
            return this._isLoading;
        }// end function

        public function startLoad() : void
        {
            var _loc_3:URLVariables = null;
            if (!this._docId)
            {
                return;
            }
            var _loc_1:* = this._docUrl + "/" + this._docId + "?pn=" + (this._docFpth + 1) + "&rn=" + this._docNpn;
            var _loc_2:* = new URLRequest(_loc_1);
            if (this._isPost && this._docBduss)
            {
                _loc_2.method = URLRequestMethod.POST;
                _loc_3 = new URLVariables();
                _loc_3.bduss = this._docBduss;
                _loc_2.data = _loc_3;
            }
            if (this._isLoading)
            {
                this.stopLoad();
            }
            this._urlStream.load(_loc_2);
            this._isLoading = true;
            dispatchEvent(new PageDataEvent(PageDataEvent.START_LOAD));
            return;
        }// end function

        public function stopLoad() : void
        {
            if (this._urlStream.connected)
            {
                this._urlStream.close();
            }
            this._isLoading = false;
            this._binaryData = new ByteArray();
            return;
        }// end function

        public function reload() : void
        {
            this.startLoad();
            return;
        }// end function

    }
}
